package com.devteam.weatherforcecast;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Calendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


import android.util.Log;

import com.devteam.weather.WeatherCurrentCondition;
import com.devteam.weather.WeatherForecastCondition;
/**
 * TODO : parse respose data from gg Api
 * Created 3.7.2012
 * @author Thanh
 */
public class GoogleWeatherHandler {
	
	private static final String tag = GoogleWeatherHandler.class.getName();
	
	/**
	 * Danh sách các dự báo của các ngày tiếp theo
	 */
	public ArrayList<WeatherForecastCondition> listWeatherForecast;
	/**
	 * Dự báo của ngày hiện tại
	 */
	public WeatherCurrentCondition weatherCurrent,weatherCurrent2;
	private String linkData;
	
	//==========================================================//
	
	/**
	 * Các hàm khởi tạo
	 */
	public GoogleWeatherHandler() {
	}
	/**
	 * @param listWeatherForecast
	 * @param weatherCurrent
	 * @param linkData
	 */
	public GoogleWeatherHandler(
			ArrayList<WeatherForecastCondition> listWeatherForecast,
			WeatherCurrentCondition weatherCurrent, String linkData) {
		this.listWeatherForecast = listWeatherForecast;
		this.weatherCurrent = weatherCurrent;
		this.linkData = linkData;
	}
	
	public void parseData() {
		// SD HTTP GET
		HttpGet getMethod = new HttpGet(linkData);
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		try {
			// Gọi URL và lấy toàn bộ file data
			String responseBody = "";
			responseBody = client.execute(getMethod, responseHandler);
			// parse the response body
			buildForecasts(responseBody);
		} catch (Exception t) {
			t.printStackTrace();
			listWeatherForecast=null;
		}
	}
	
	private void buildForecasts(String raw) throws ParserConfigurationException, Exception,
			Exception {
		// Using the DOM API: Create the document builder
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		// Create the Dom Document by parsing the HTTP response
		Document doc = builder.parse(new InputSource(new StringReader(raw)));
		
		// ================== Thời tiết của ngày các ngày tiếp theo
		
		// Load the nodeList associated to the forcast_condition nodes
		NodeList forecastList = doc.getElementsByTagName("forecast_conditions");
		for (int i = 0; i < forecastList.getLength(); i++) {
			Element currentFore = (Element) forecastList.item(i);
			String day = currentFore.getElementsByTagName("day_of_week").item(0).getAttributes()
					.item(0).getNodeValue();
			String nhietDoMin = currentFore.getElementsByTagName("low").item(0).getAttributes()
					.item(0).getNodeValue();
			String nhietDoMax = currentFore.getElementsByTagName("high").item(0).getAttributes()
					.item(0).getNodeValue();
			String icon = currentFore.getElementsByTagName("icon").item(0).getAttributes()
					.item(0).getNodeValue();
			String tinhTrang = currentFore.getElementsByTagName("condition").item(0).getAttributes()
					.item(0).getNodeValue();
			WeatherForecastCondition a = new WeatherForecastCondition(day,
					Integer.parseInt(nhietDoMin), Integer.parseInt(nhietDoMax),
					icon, tinhTrang);
			
			listWeatherForecast.add(a);
		}
		
		//================== Thời tiết của ngày hiện tại
		
		NodeList currentWeather = doc.getElementsByTagName("current_conditions");
		Element element = (Element) currentWeather.item(0);		
		//
		String tinhTrangHienTai = element.getElementsByTagName("condition").item(0).getAttributes()
				.item(0).getNodeValue();
		//
		String doF= element.getElementsByTagName("temp_f").item(0).getAttributes()
				.item(0).getNodeValue();
		//
		String doC = element.getElementsByTagName("temp_c").item(0).getAttributes()
				.item(0).getNodeValue();
		//
		String doAm = element.getElementsByTagName("humidity").item(0).getAttributes()
				.item(0).getNodeValue();
		//
		String linkIcon = element.getElementsByTagName("icon").item(0).getAttributes()
				.item(0).getNodeValue();
		//
		String tinhTrangGio = element.getElementsByTagName("wind_condition").item(0).getAttributes()
				.item(0).getNodeValue();
		Calendar ci = Calendar.getInstance();
		int a=ci.get(Calendar.MONTH)+1;
		String t = ci.get(Calendar.DAY_OF_MONTH) + "."
		+ a + "."
		+ ci.get(Calendar.YEAR);
		weatherCurrent2=new WeatherCurrentCondition(t,Integer.parseInt(doC),
				Integer.parseInt(doF),linkIcon, tinhTrangHienTai,
				tinhTrangGio, doAm);
		weatherCurrent = new WeatherCurrentCondition("", Integer.parseInt(doC),
				Integer.parseInt(doF), linkIcon, tinhTrangHienTai,
				tinhTrangGio, doAm);
	}
	
	public WeatherCurrentCondition getWeatherCurrentCondition()
	{
		return weatherCurrent;
	}
	

}
